//
//  HTTabbar.swift
//  weibo
//
//  Created by JonsonLiu on 2017/2/22.
//  Copyright © 2017年 JonsonLiu. All rights reserved.
//

import UIKit

class HTTabbar: UITabBar {
    
    
    var composeButtonClick : (()->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(commosButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        commosButton.center = CGPoint(x:self.frame.width/2, y:self.frame.height/4)
        
        //得到tabbar的宽度
        let itemW = self.frame.width / 5
        
        //设置一个角标来记录每个UITabBarButton
        var index = 0
        
        //遍历取出每个UITabBarButton
        for value in subviews{
            
            //判断是不是这个类注:  swift3.0 API 有改变
            if value.isKind(of: NSClassFromString("UITabBarButton")!){
                
                //每个角标的宽度 = 取出的宽度
                value.frame.size.width = itemW
                
                //每个UITabBarButton 的X值
                value.frame.origin.x = CGFloat(index) * itemW
                
                index += 1
                
                if index == 2 {
                    index += 1
                    
                    
                }
//                if index == 3 {
//                    value.frame.origin.y = -20
//                }
            }
        }
    }
    

    private lazy var commosButton : UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: #selector(HTTabbar.composeButtonClicknOW), for: .touchUpInside)
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .highlighted)
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), for: .normal)
        
        //设置不同状态下的背景图片
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: .normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        //内容多大 控件就多大
        button.sizeToFit()
        
        return button
        
    }()
    
    func composeButtonClicknOW(){
        
        composeButtonClick?()
    }

}



